home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_04 / bugg / spin.c < prev    next >
C/C++ Source or Header  |  1994-02-08  |  14KB  |  503 lines

  1. //    *** LISTING 1
  2. //  **************************************************
  3. //  * NAME = SPIN.C                                  *
  4. //  * DEV. PLATFORM=  Turbo C++,v3.1 for Windows     *
  5. //    * MEMORY MODEL =  SMALL                          *
  6. //    * Demo. program for creating/using spin controls.*
  7. //  * Keith E. Bugg, TRISTAR SYSTEMS, Inc            *
  8. //  **************************************************
  9. #define OEMRESOURCE    // MUST define BEFORE <windows.h>
  10. #define DAY_CTRL 100// Ctrl IDs for day editbox ctrl.
  11. #define MONTH_CTRL 200    // ID for month combobox ctrl.
  12. #define YEAR_CTRL 300    // ID for year editbox ctrl.
  13. #include <windows.h>// must have Windows header file
  14. #include <stdio.h>    // get basic prototypes
  15. #include <stdlib.h>    // for 'itoa()', etc.
  16.  
  17.         /* declare prototypes here  */
  18. long FAR PASCAL _export SpinProc(HWND, unsigned,
  19.     WORD, LONG);
  20. BOOL FAR PASCAL DateDialog(HWND ,WORD,WORD ,LONG );
  21. //
  22. //      GLOBAL VARIABLES HERE
  23. //
  24. HDC hDC;        // handle to output Display Context
  25. int day,month,year;
  26. char *months[]= {"January","Febuary","March","April",
  27.     "May","June","July","August","September","October",
  28.     "November" ,"December"};
  29. int max_days[]= {31,28,31,30,31,30,31,31.30,31,30,31};
  30. char dayval[3],yearval[5];
  31. //  **************************************************
  32. //    end var. decl., etc.  Now do message handler for
  33. //    the main window of the spin control demo program
  34. //  **************************************************
  35. //
  36. long FAR PASCAL _export SpinProc(HWND hWnd,
  37.     unsigned message,WORD wParam, LONG lParam)
  38. {
  39.     PAINTSTRUCT ps; // Paint Struct for BeginPaint call
  40.     int i;                    // general purpose variable
  41.     HINSTANCE hInstance;
  42.     FARPROC lpfnDlgProc; 
  43.     // --------- end local variables ---------------
  44.     //
  45.     switch (message)    // process messages here
  46.     {
  47.  
  48.         case WM_CLOSE:     //  Exit via system menu
  49.             MessageBeep(0);        // Warning beep
  50.             i= MessageBox(hWnd,
  51.             "Are you sure you want to Exit?",
  52.             "EXIT",MB_OKCANCEL | MB_ICONEXCLAMATION);
  53.             if(i == IDOK)      // really wants to exit 
  54.             {                  // queue up a QUIT msg
  55.                 PostMessage(hWnd,WM_QUIT,0,0); 
  56.                 return 0L;
  57.             }
  58.                break;
  59.     
  60.         case WM_COMMAND:     // check for system message
  61.             switch(wParam)
  62.             {
  63.                 case SC_MINIMIZE:   //  on minimize
  64.                     ShowWindow(hWnd,SW_SHOWMINIMIZED);
  65.                     break;
  66.             
  67.                 case SC_MAXIMIZE:   //  on maximize
  68.                     MessageBeep(0); //stub
  69.                     ShowWindow(hWnd,SW_SHOWMAXIMIZED);
  70.                     break;
  71.  
  72.                 case SC_RESTORE:    //  on restore
  73.                     ShowWindow(hWnd,SW_SHOW);
  74.                     break;
  75.                 //
  76.                 //    here is user clicks the sub-menu
  77.                 //    option "Run Demo" from drop-down
  78.                 case 100:    //  Run Demo menu option
  79.                     hInstance = GetWindowWord(hWnd,
  80.                         GWW_HINSTANCE);
  81.                     lpfnDlgProc= MakeProcInstance(
  82.                         DateDialog,hInstance);
  83.                     DialogBox(hInstance,"DIALOG_1",
  84.                         hWnd,lpfnDlgProc);
  85.                     break;
  86.  
  87.                 default:
  88.                     break;
  89.             }
  90.             break;
  91.         
  92.         case WM_QUIT:       // QUIT & DESTROY messages
  93.         case WM_DESTROY: 
  94.             return (0L);
  95.  
  96.         default:          // message is of no interest
  97.             return (DefWindowProc(hWnd, message,
  98.                 wParam, lParam));
  99.     }
  100.     return (NULL);
  101. }       /* end SpinProc()    */
  102.  
  103. #pragma argsused        // TC++ compiler directive
  104. //
  105. //    next comes WinMain, which builds demo window
  106. //
  107. //****************************************************
  108. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE
  109.     hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
  110. {
  111.     MSG msg;            // message
  112.     WNDCLASS  wc;
  113.     static HWND hWnd;
  114.     //
  115.     // ----------------------------------------------
  116.     //
  117.     
  118.     if (!hPrevInstance) // Other instances running?
  119.     {
  120.         // Fill in window class structure with
  121.         // parameters that describe the main window.
  122.         //
  123.         wc.style = CS_HREDRAW | CS_VREDRAW;  
  124.         wc.lpfnWndProc=(long(FAR PASCAL*)())SpinProc;
  125.         wc.cbClsExtra = 0; // No per-class extra data.
  126.         wc.cbWndExtra = 0; // No per-window extra data.
  127.         wc.hInstance = hInstance;
  128.         wc.hIcon = LoadIcon(hInstance, "spin");;
  129.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  130.         wc.hbrBackground = CreateSolidBrush(
  131.             RGB(100,255,255)); // cyan background
  132.         wc.lpszMenuName = "MAIN_MENU";
  133.         wc.lpszClassName = "SPIN";
  134.  
  135.         // Register the window class and return
  136.         // success/failure code. 
  137.  
  138.         if(!RegisterClass(&wc))
  139.             return 0;
  140.  
  141.         // Create main window for application instance.
  142.  
  143.         hWnd = CreateWindow(
  144.             "SPIN",       // See RegisterClass() call.
  145.             "Spin Controls",// Text for title bar.
  146.             WS_MAXIMIZE | WS_SYSMENU | WS_MINIMIZEBOX |
  147.             WS_MAXIMIZEBOX | WS_THICKFRAME |
  148.             WS_OVERLAPPEDWINDOW,// Window's styles..
  149.             0,                     // horizontal position.
  150.             0,                    // vertical position.
  151.             600,                  // Default width.
  152.             400,                 // Default height.
  153.             NULL,               // no parent.
  154.             NULL,               // No menu bar (later)
  155.             hInstance,          // inst. owns window.
  156.             NULL                // Pointer not needed.
  157.             );               
  158.         //
  159.         // If CreateWindow failed, return "failure" */
  160.         if (!hWnd)
  161.         {        
  162.             MessageBeep(0);
  163.             MessageBox(hWnd,"Could not create window!",
  164.                 "ERROR",MB_OK);
  165.             return 0;
  166.         }
  167.  
  168.         // Make window visible; update its client area
  169.  
  170.         nCmdShow= SW_SHOWNORMAL;    // Show normal size
  171.         ShowWindow(hWnd,nCmdShow ); // Show the window
  172.         UpdateWindow(hWnd);         // Send WM_PAINT 
  173.         
  174.     }
  175.     //     Acquire and dispatch messages until a
  176.     //    WM_QUIT message is received.
  177.  
  178.     while (GetMessage(&msg, // message structure
  179.         NULL,               // window handle rec. msg
  180.         NULL,               // lowest msg. to examine
  181.         NULL))              // highest msg. to examine
  182.     {
  183.         TranslateMessage(&msg); // Translates msgs.
  184.         DispatchMessage(&msg);  // Dispatches msgs. 
  185.     }
  186.     return (msg.wParam);    // Returns the value
  187.                             // from PostQuitMessage
  188. }               // end WinMain()
  189.  
  190. BOOL FAR PASCAL DateDialog(HWND hDlg,WORD wMessage,
  191.     WORD wParam, LONG lParam)
  192. {
  193.     static HWND hDay,hCombo,hYear;
  194.     HANDLE hInstance;    // app. instance
  195.     //
  196.     //    decl. rectangles for bit maps
  197.     //
  198.     static RECT day_up,day_down,year_up, year_down;
  199.     static HDC hMemDC;    // handle to memory DC        
  200.     static BITMAP bm;    // for loading bit maps
  201.     static HBITMAP    hUp,hDown;    // bit map handles
  202.     MSG msg;        // message struct. for slewing
  203.     int k, mon_max;    // mon_max is max # days in month    
  204.     static int bmWidth, bmHeight; // bit map size
  205.     POINT pt;    // for finding mouse click in rects.
  206.     //
  207.     switch(wMessage)
  208.     {
  209.         // initialize dialog box here
  210.         case WM_INITDIALOG:        
  211.             hInstance = GetWindowWord(hDlg,
  212.                 GWW_HINSTANCE);
  213.             //
  214.             //    create Day edit box as child window
  215.             //
  216.             hDay = CreateWindow("EDIT",NULL,ES_LEFT |
  217.                    ES_READONLY | WS_CHILD | WS_BORDER
  218.                    | WS_VISIBLE | WS_TABSTOP,40,55,50,
  219.                    25,hDlg,DAY_CTRL,hInstance,NULL);
  220.             //
  221.             //    create Month combobox as child window
  222.             //
  223.             hCombo = CreateWindow("COMBOBOX",NULL,
  224.                      WS_CHILD | WS_VSCROLL | WS_TABSTOP
  225.                      | CBS_DROPDOWN | CBS_HASSTRINGS,
  226.                      175, 55, 120, 150,hDlg,MONTH_CTRL,
  227.                      hInstance,NULL);
  228.             //
  229.             //    create Year edit box as child window
  230.             //
  231.             hYear = CreateWindow("EDIT",NULL,ES_LEFT |
  232.                     ES_READONLY | WS_CHILD | WS_VISIBLE
  233.                     | WS_BORDER | WS_TABSTOP,
  234.                     310,55,70,25,hDlg,YEAR_CTRL,
  235.                     hInstance,NULL);
  236.             ShowWindow(hYear,SW_SHOWNORMAL);
  237.             //
  238.             // init. combobox with names of months
  239.             //
  240.             for(k=0; k < 12; k++)    
  241.                 SendMessage(hCombo,CB_ADDSTRING,0,
  242.                     (DWORD)(LPSTR)months[k]);
  243.                 
  244.             ShowWindow(hCombo,SW_SHOWNORMAL);
  245.             //
  246.             // Load the Microsoft-supplied arrows here
  247.             //
  248.             hUp = LoadBitmap(NULL,OBM_UPARROW);
  249.             hDown = LoadBitmap(NULL,OBM_DNARROW); 
  250.             //
  251.             //    Get the height & width of the up/down
  252.             //  arrows supplied by Microsoft.  Use
  253.             //  this to set up rectangles upon which
  254.             //    the arrows will be "pasted".  When
  255.             //  user clicks mouse inside one of these
  256.             //  rectangles, we change the day/year
  257.             //  
  258.             GetObject(hUp, sizeof(BITMAP), &bm);
  259.             // get size of up/down arrow (same)
  260.             bmWidth = bm.bmWidth;    
  261.                bmHeight= bm.bmHeight;
  262.             //
  263.             // memory handle to DC;